<template>
  <el-row :gutter="40" class="panel-group">
    <el-col
      :xs="24"
      :sm="24"
      :lg="24"
      class="card-panel-col-title"
      style="padding: 0;"
    >
      <div class="sigma-content">
        <div class="sigma-middle-line">
          <div class="sigma-line-text">
            <span class="sigma-title">数据概况</span>
            <div style="float:right;">
              <el-button
                :type="tip == 'today' ? 'primary' : ''"
                style="width:100px;border-radius:2px;"
                @click="today()"
                >今日</el-button
              >
              <el-button
                :type="tip == 'yesterday' ? 'primary' : ''"
                style="width:100px;border-radius:2px;"
                @click="yesterday()"
                >昨日</el-button
              >
              <el-button
                :type="tip == 'sevenDays' ? 'primary' : ''"
                style="width:100px;border-radius:2px;margin-right: 7px;"
                @click="sevenDays()"
                >近七日</el-button
              >
              <el-date-picker
                v-model="times"
                class="filter-item"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                style="width: 250px;"
                type="daterange"
                value-format="yyyy-MM-dd"
                @change="handleQuery()"
              />
            </div>
          </div>
        </div>
      </div>
    </el-col>

    <el-col
      :xs="24"
      :sm="24"
      :lg="24"
      :loading="loading"
      class="card-panel-col elLoading"
      style="padding: 16px;"
    >
      <div class="card-panel" v-for="(item, index) in ROI" :key="index">
        <div class="card-panel-text">
          {{ item.name }}
          <el-popover
            v-if="item.name == '推送率'"
            placement="top"
            width="200"
            trigger="hover"
            content="推送率=机构匹配量/表单量"
          >
            <i class="el-icon-question" slot="reference"></i>
          </el-popover>
        </div>
        <div
          v-if="item.name == 'ROI'"
          :class="['card-panel-num', item.data >= 1 ? 'straight' : 'burden']"
        >
          {{ item.data }}
        </div>
        <div v-else :class="['card-panel-num', 'straight']">
          {{ item.data }}
        </div>
      </div>
    </el-col>
  </el-row>
</template>

<script>
import { getRoi } from '@/api/home';
import { timestampToTime } from '@/utils/index';
import { Loading } from 'element-ui';
export default {
  data() {
    return {
      listQuery: {
        startTime: '',
        endTime: ''
      },
      times: [],
      timeNow: [
        new Date(new Date().toLocaleDateString()).getTime(),
        new Date(new Date().toLocaleDateString()).getTime() +
          (24 * 60 * 60 * 1000 - 1)
      ],
      ROI: [],
      tip: 'today',
      loading: true
    };
  },
  created() {
    this.times = [
      timestampToTime(this.timeNow[0]),
      timestampToTime(this.timeNow[1])
    ];
    this.listQuery.endTime = this.times[1];
    this.listQuery.startTime = this.times[0];
    this.handleSetLineChartData(this.listQuery);
  },
  methods: {
    handleQuery() {
      this.listQuery.startTime = this.times[0] + ' 00:00:00';
      this.listQuery.endTime = this.times[1] + ' 23:59:59';
      this.tip = '';
      this.handleSetLineChartData(this.listQuery);
    },
    today() {
      this.times = [
        timestampToTime(this.timeNow[0]),
        timestampToTime(this.timeNow[1])
      ];
      this.listQuery.startTime = this.times[0];
      this.listQuery.endTime = this.times[1];

      this.tip = 'today';
      this.handleSetLineChartData(this.listQuery);
    },
    yesterday() {
      this.times = [
        timestampToTime(this.timeNow[0] - 24 * 60 * 60 * 1000),
        timestampToTime(this.timeNow[1] - 24 * 60 * 60 * 1000)
      ];
      this.listQuery.startTime = this.times[0];
      this.listQuery.endTime = this.times[1];
      this.tip = 'yesterday';
      this.handleSetLineChartData(this.listQuery);
    },
    sevenDays() {
      this.times = [
        timestampToTime(this.timeNow[0] - 24 * 60 * 60 * 1000 * 6),
        timestampToTime(this.timeNow[1])
      ];
      this.listQuery.endTime = this.times[1];
      this.listQuery.startTime = this.times[0];
      this.tip = 'sevenDays';
      this.handleSetLineChartData(this.listQuery);
    },
    async getRoi() {
      const res = await getRoi(this.listQuery);
      if (res.data.code === 200) {
        this.ROI = res.data.data;
      }
    },
    handleSetLineChartData(list) {
      this.$emit('handleSetLineChartData', list);
      this.getRoi();
    }
  }
};
</script>

<style lang="scss" scoped>
.panel-group {
  margin: 0 !important;
  margin-left: -10px !important;

  .card-panel-col-title {
    margin-bottom: 16px;
  }
  .card-panel-col {
    margin-bottom: 16px;
    background: #ffffff;
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-radius: 2px;
  }
  .card-panel {
    font-size: 12px;
    position: relative;
    overflow: hidden;
    background: #ffffff;
    .icon-people {
      color: #40c9c6;
    }
    .icon-message {
      color: #36a3f7;
    }
    .icon-shopping {
      color: #34bfa3;
    }
    .card-panel-icon-wrapper {
      float: left;
      margin: 14px 0 0 14px;
      padding: 16px;
      transition: all 0.38s ease-out;
      border-radius: 6px;
    }
    .card-panel-text {
      line-height: 14px;
      color: #354768;
      font-size: 14px;
      margin-bottom: 12px;
    }
    .card-panel-num {
      font-size: 20px;
    }
    .straight {
      color: #376df7;
    }
    .burden {
      color: #67c23a;
    }
  }
}

@media (max-width: 550px) {
  .card-panel-description {
    display: none;
  }
  .card-panel-icon-wrapper {
    float: none !important;
    width: 100%;
    height: 100%;
    margin: 0 !important;
    .svg-icon {
      display: block;
      margin: 14px auto !important;
      float: none !important;
    }
  }
}
.vip-p {
  text-align: left;
  color: #354768;
  font-size: 16px;
  padding: 5px 0;
}
.vip-title {
  color: #8f97a7;
}
.vip-title::before {
  content: '';
  display: inline-block;
  vertical-align: 3px;
  margin-right: 10px;
  background: #167bff;
  width: 6px;
  height: 6px;
  border-radius: 3px;
}
.sigma-content {
  background-color: #ffffff;
  // height: 76px;
  // line-height: 76px;
  padding: 16px 0;
}
.sigma-title {
  position: absolute;
  top: 13px;
  left: 16px;
  width: 100px;
  font-size: 16px;
  border-left: 3px solid #376df7;
  padding: 0 4px;
  margin: 2px 0;
  line-height: 10px;
}
.sigma-line-text {
  width: 100%;
  display: inline-block;
  position: relative;
  padding: 0 14px;
  font-size: 14px;
  font-weight: 500;
}
</style>
